home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Classes / 2.0_HotShape / beta / HotShape.m < prev    next >
Encoding:
Text File  |  1992-07-26  |  3.0 KB  |  157 lines

  1. // HotShape.m
  2. // By Charles G. Fleming, Educational Computing Services, Allegheny College.
  3. // Copyright 1992 Allegheny College
  4. // You may freely copy, distribute and reuse this code. 
  5. // Allegheny College and the author disclaim any warranty of any kind, 
  6. // expressed or implied, as to its fitness for any particular use.
  7. // This work was partially supported by a grant from the Vira Heinz Endowment.
  8.  
  9. #import "HotShape.h"
  10. #import "HotPath.h"
  11.  
  12. #import "SmallCell.h"
  13.  
  14. #import <appkit/Application.h>
  15.  
  16. #import <dpsclient/wraps.h>
  17.  
  18. #import <stdlib.h>
  19. #import <strings.h>
  20.  
  21. #ifndef    MIN
  22. #define    MIN(a,b) (((a)<(b))?(a):(b))
  23. #endif    MIN
  24.  
  25. @implementation HotShape
  26.  
  27. // Scale the HotShape object so that it has unit length and height.
  28. // Give it a custom cell so that it can be resized to an arbitrarily small
  29. // size in Interface Builder.  The default shape will be a diamond shape.
  30. - initFrame:(const NXRect *)frameRect
  31. {
  32.     SmallCell *smallCell;
  33.     
  34.     self = [super initFrame:frameRect];
  35.     
  36.     hotPathName = malloc(1);
  37.     visible = YES;
  38.     [self scale:bounds.size.width :bounds.size.height];
  39.  
  40.     smallCell = [[SmallCell allocFromZone:[self zone]] init];
  41.     [self setCell:smallCell];
  42.  
  43.     setHotPaths();
  44.     tag = 0;
  45.     [self setHotPathName:"diamond"];
  46.     return self;
  47. }
  48.  
  49. - (BOOL)acceptsFirstMouse
  50. {
  51.     return YES;
  52. }
  53.     
  54. - drawSelf:(const NXRect *)rects :(int)rectCount
  55. {
  56.     drawHotPath(hotPathName, visible);
  57.     return self;
  58. }    
  59.  
  60. - mouseDown:(NXEvent *)theEvent
  61. {
  62.     SEL action;
  63.     NXEvent *nextEvent;
  64.     BOOL done = NO;    
  65.     int inRegion;
  66.     NXPoint viewCoords = {theEvent->location.x, theEvent->location.y};
  67.     
  68.     [self convertPoint:&viewCoords fromView:nil];
  69.     [self lockFocus];    
  70.     inHotRegion(hotPathName, viewCoords.x, viewCoords.y, &inRegion);
  71.     [self unlockFocus];    
  72.  
  73.     if(inRegion)
  74.         while(!done)
  75.         {
  76.             nextEvent = [NXApp getNextEvent:NX_MOUSEUPMASK];
  77.             if(nextEvent->type == NX_MOUSEUP)
  78.             {
  79.                 viewCoords.x = nextEvent->location.x;
  80.                 viewCoords.y = nextEvent->location.y;
  81.                 [self convertPoint:&viewCoords fromView:nil];
  82.                 
  83.                 [self lockFocus];    
  84.                 inHotRegion(hotPathName, viewCoords.x, viewCoords.y,
  85.                         &inRegion);
  86.                 [self unlockFocus];    
  87.                 
  88.                 if(inRegion)
  89.                 {
  90.                     action = [cell action];
  91.                     if(action)
  92.                         [self sendAction:action to:[cell target]];
  93.                 }
  94.                 done = YES;    
  95.             }
  96.         }                
  97.     return self;    
  98. }
  99.  
  100. - awake
  101. {
  102.     setHotPaths();
  103.     return self;
  104. }
  105.  
  106. - sizeTo:(NXCoord)width :(NXCoord)height
  107. {
  108.     [super sizeTo:width :height];
  109.     [self scale:bounds.size.width :bounds.size.height];
  110.     return self;
  111. }
  112.  
  113. - setHotPathName:(char *)name
  114. {
  115.     free(hotPathName);
  116.     hotPathName = malloc(strlen(name) + 1);
  117.     strcpy(hotPathName, name);    
  118.     return self;
  119. }    
  120.  
  121. - (char *)hotPathName
  122. {
  123.     return hotPathName;
  124. }
  125.         
  126. - (const char *)inspectorName
  127. {
  128.     return "HotShapeInspector";
  129. }    
  130.  
  131. - (BOOL)visible
  132. {
  133.     return visible;
  134. }
  135.  
  136. - setVisible:(BOOL)seeIt
  137. {
  138.     visible = seeIt;
  139.     return self;
  140. }    
  141.     
  142. - read:(NXTypedStream *)typedStream
  143. {
  144.     [super read:typedStream];
  145.     NXReadTypes(typedStream, "*s", &hotPathName, &visible);
  146.     return self;
  147. }        
  148.  
  149. - write:(NXTypedStream *)typedStream
  150. {
  151.     [super write:typedStream];
  152.     
  153.     NXWriteTypes(typedStream, "*s", &hotPathName, &visible);
  154.     return self;
  155. }        
  156. @end
  157.